//
//  HEDocTeamServerListViewController.m
//  HealthElderly
//
//  Created by 吴欧 on 2021/6/30.
//  Copyright © 2021 honglutec. All rights reserved.
//

#import "HEDocTeamServerListViewController.h"
#import "HESickedModel.h"
#import "DLRadioButton.h"
#import "HESheetListView.h"
#import "HEApplyModel.h"
#import "HESickedCreateOrderViewController.h"
#import "HESickedModel.h"
#import "HESickbedPlanItemCell.h"
#import "HESickbedDetailViewController.h"

@interface HEDocTeamServerListViewController ()<HESheetListViewDelegate , UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) GDTableView *tableView;

@property (nonatomic, strong) HESickedModel *sickedModel;

@property (nonatomic, strong) UIScrollView *scrollView;

@property (nonatomic, strong) UILabel *resLb;

@property (nonatomic, strong) QMUIFillButton *applyBtn;

// 约束参照视图,也是容器视图
@property (nonatomic, strong) UIView *contentView;

@property (nonatomic, strong) DLRadioButton *radioBtn;

/// 服务机构
@property (nonatomic, strong) NSMutableArray <HESickedPlanModel *>*servers;

@end

@implementation HEDocTeamServerListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"团队服务内容";
    
    [self loadData];
}

#pragma mark - load data
- (void)loadData {
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    [params setValue:_teamId forKey: @"groupId"];
    
    [WOAFNetWorkingHandle POSTWithBaseUrl:KServerUrl suffixUrl:KinstTeamService parameters: params success:^(id  _Nonnull responseObject) {
        DLog(@"%@", [responseObject mj_JSONString])
        HEArrayResponse *respone = [HEArrayResponse mj_objectWithKeyValues:responseObject];
        self.servers = [NSMutableArray arrayWithArray:[HESickedPlanModel mj_objectArrayWithKeyValuesArray:respone.data]];
        [self initServerListView];

    } failure:^(NSError * _Nonnull error) {
        
    }];
}

#pragma mark -

- (void)initServerListView {
    [self.view addSubview:self.tableView];
}

- (GDTableView *)tableView {
    if (!_tableView) {
        _tableView = [[GDTableView alloc] initWithFrame:CGRectMake(0, ZXNavBarHeight + 50 + 50, KScreenWidth, KScreenHeight - ZXNavBarHeight -50 -50) style:UITableViewStylePlain];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
        _tableView.separatorColor = CSplitLineColor;
        _tableView.scrollsToTop = YES;
        _tableView.tableFooterView = [[UIView alloc] init];
        _tableView.backgroundColor = KWhiteColor;
        _tableView.delegate = self;
        _tableView.dataSource = self;
         
        [_tableView registerClass:[HESickbedPlanItemCell class] forCellReuseIdentifier: @"HESickbedPlanItemCell"];
        
        [self.view addSubview:_tableView];
        
        [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.view).mas_offset(ZXNavBarHeight);
            make.left.equalTo(self.view);
            make.right.equalTo(self.view);
            make.bottom.equalTo(self.view).offset(-ZXSafeAreaBottom);
        }];
    }
    return _tableView;
}
 
#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _servers.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    HESickbedPlanItemCell *cell = [_tableView dequeueReusableCellWithIdentifier: @"HESickbedPlanItemCell"];
    HESickedPlanModel *sickbedModel = _servers[indexPath.row];
    cell.serverNameLb.text = sickbedModel.service_name;
    cell.serverDateLb.text = sickbedModel.service_cnt;
    cell.serverStateLb.text = sickbedModel.used_cnt;

    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    HESickedPlanModel *sickbedModel = _servers[indexPath.row];

    return [tableView fd_heightForCellWithIdentifier: @"HESickbedPlanItemCell" cacheByIndexPath:indexPath configuration:^(HESickbedPlanItemCell *cell) {
        cell.serverNameLb.text = sickbedModel.service_name;
        cell.serverDateLb.text = sickbedModel.service_cnt;
        cell.serverStateLb.text = sickbedModel.used_cnt;
    }];
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *warp = [UIView new];
    warp.backgroundColor = CViewBgColor;
    warp.qmui_borderWidth = 1;
    warp.qmui_borderColor = CViewBgColor;
    warp.qmui_borderPosition = QMUIViewBorderPositionBottom;

    
    UILabel *serverNameLb = [UILabel new];
    serverNameLb.text = @"服务项目";
    serverNameLb.font = DYBlodFont(17);
    serverNameLb.textAlignment = NSTextAlignmentCenter;
    [warp addSubview:serverNameLb];
    
    UILabel *serverDateLb = [UILabel new];
    serverDateLb.text = @"服务次数";
    serverDateLb.font = DYBlodFont(17);
    serverDateLb.textAlignment = NSTextAlignmentCenter;
    [warp addSubview:serverDateLb];
    
    UILabel *serverStateLb = [UILabel new];
    serverStateLb.text = @"已用次数";
    serverStateLb.font = DYBlodFont(17);
    serverStateLb.textAlignment = NSTextAlignmentCenter;
    [warp addSubview:serverStateLb];
    
    UIView *line1 = [UIView new];
    line1.backgroundColor = CNavSubTitleColor;
    [warp addSubview:line1];
    
    UIView *line2 = [UIView new];
    line2.backgroundColor = CNavSubTitleColor;
    [warp addSubview:line2];
    
    [serverNameLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(warp);
        make.left.equalTo(warp);
        make.width.mas_equalTo(warp.mas_width).multipliedBy(0.4);
        make.bottom.equalTo(warp);
    }];
    
    [serverDateLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(warp);
        make.left.equalTo(serverNameLb.mas_right);
        make.width.mas_equalTo(warp.mas_width).multipliedBy(0.35);
        make.bottom.equalTo(warp);
    }];
    
    [serverStateLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(warp);
        make.left.equalTo(serverDateLb.mas_right);
        make.right.equalTo(warp);
        make.width.mas_equalTo(warp.mas_width).multipliedBy(0.25);
        make.bottom.equalTo(warp);
    }];
 
     
    [line1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(warp);
        make.left.equalTo(serverNameLb.mas_right).offset(-1);
        make.width.mas_equalTo(1);
        make.height.mas_equalTo(20);
    }];
    
    [line2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(warp);
        make.left.equalTo(serverDateLb.mas_right).offset(-1);
        make.width.mas_equalTo(1);
        make.height.mas_equalTo(20);
    }];
  
    
    return warp;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 50;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [_tableView deselectRowAtIndexPath:indexPath animated:YES];
}
 
#pragma mark - getters
// scrollView
- (UIScrollView *)scrollView {
    if (_scrollView == nil) {
        _scrollView = [[UIScrollView alloc] init];
        _scrollView.backgroundColor = KWhiteColor;
        _scrollView.alwaysBounceVertical = YES;
    }
    return _scrollView;
}

// 约束参照视图
- (UIView *)contentView {
    if (_contentView == nil) {
        _contentView = [[UIView alloc] init];
        _contentView.backgroundColor = KWhiteColor;
    }
    return _contentView;
}

@end
